package com.li.tree.demo1;


public class BinaryTreeDemo {
    public static void main(String[] args) {
        BinaryTree binaryTree = new BinaryTree();
        HeroNode1 root = new HeroNode1(1, "jack");
        HeroNode1 node2 = new HeroNode1(3, "tom");
        HeroNode1 node3 = new HeroNode1(6, "mike");
        HeroNode1 node4 = new HeroNode1(8, "林冲");
        HeroNode1 node5 = new HeroNode1(10, "关胜");
        HeroNode1 node6 = new HeroNode1(14, "jerry");
        root.setLeft(node2);
        root.setRight(node3);
        node2.setLeft(node4);
        node2.setRight(node5);
        node3.setLeft(node6);
        binaryTree.setRoot(root);
        binaryTree.threadNodes();
        HeroNode1 left = node5.getLeft();
        System.out.println(left);
        binaryTree.threadedList();


    }

}

class BinaryTree {
    private HeroNode1 root;
    private HeroNode1 pre = null;

    public HeroNode1 getRoot() {
        return root;
    }

    public void setRoot(HeroNode1 root) {
        this.root = root;
    }

    public HeroNode1 getPre() {
        return pre;
    }

    public void setPre(HeroNode1 pre) {
        this.pre = pre;
    }

    public void threadNodes() {
        threadNodes(root);
    }

    public void threadNodes(HeroNode1 node) {
        if (node == null) {
            return;
        }
        threadNodes(node.getLeft());
        if (node.getLeft() == null) {
            node.setLeft(pre);
            node.setLeftType(1);
        }
        if (pre != null && pre.getRight() == null) {
            pre.setRight(node);
            pre.setRightType(1);
        }
        pre = node;
        threadNodes(node.getRight());
    }
    // 遍历线索化二叉树的方法
    public void threadedList() {
        // 定义一个变量，存储当前遍历的结点，从root开始
        HeroNode1 node = root;
        while (node != null) {
            // 循环的找到leftType == 1的结点，第一个找到就是8结点
            // 后面随着遍历而变化,因为当leftType==1时，说明该结点是按照线索化处理后的有效结点
            while (node.getLeftType() == 0) {
                node = node.getLeft();
            }

            // 打印当前这个结点
            System.out.println(node);
            // 如果当前结点的右指针指向的是后继结点,就一直输出
            while (node.getRightType() == 1) {
                // 获取到当前结点的后继结点
                node = node.getRight();
                System.out.println(node);
            }
            // 替换这个遍历的结点
            node = node.getRight();

        }
    }
}

class HeroNode1 {
    private int no;
    private String name;
    private HeroNode1 left;
    private HeroNode1 right;
    // 说明
    // 1. 如果leftType == 0 表示指向的是左子树, 如果 1 则表示指向前驱结点
    // 2. 如果rightType == 0 表示指向是右子树, 如果 1表示指向后继结点
    private int leftType;
    private int rightType;

    public int getLeftType() {
        return leftType;
    }

    public void setLeftType(int leftType) {
        this.leftType = leftType;
    }

    public int getRightType() {
        return rightType;
    }

    public void setRightType(int rightType) {
        this.rightType = rightType;
    }

    public HeroNode1(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public HeroNode1 getLeft() {
        return left;
    }

    public void setLeft(HeroNode1 left) {
        this.left = left;
    }

    public HeroNode1 getRight() {
        return right;
    }

    public void setRight(HeroNode1 right) {
        this.right = right;
    }


    @Override
    public String toString() {
        return "HeroNode1{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }
}

